home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / colourtoalpha < prev    next >
Encoding:
Text File  |  2000-05-21  |  2.0 KB  |  80 lines

  1. #!/usr/bin/perl -w
  2.  
  3. use Gimp::Feature 'pdl';
  4. use Gimp 1.091;
  5. use Gimp::Fu;
  6. use PDL::LiteF;
  7.  
  8. register "colour_to_alpha",
  9.          "Converts the specified colour to alpha",
  10.          "This replaces as much as possible of the specified colour in each pixel by a corresponding "
  11.          ."amount of alpha, then readjusts the colour accordingly.",
  12.          "Marc Lehmann",
  13.          "Marc Lehmann <pcg\@goof.com>",
  14.          "19990729",
  15.          N_"<Image>/Filters/Colors/Colour To Alpha...",
  16.          "RGB*",    
  17.          [
  18.            [PF_COLOR,        "colour",    "The colour to replace"],
  19.          ],
  20.          sub {                    # es folgt das eigentliche Skript...
  21.    my($image,$drawable,$colour)=@_;
  22.  
  23.    $drawable->is_layer or die "colour_to_alpha only works with layers\n";
  24.    $drawable->add_alpha unless $drawable->has_alpha;
  25.  
  26.    Gimp->progress_init ("Replacing colour...");
  27.  
  28.    my @bounds = $drawable->bounds;
  29.    {
  30.       # $src and $dst must either be scoped or explicitly undef'ed
  31.       # before merge_shadow.
  32.       my $src = new PixelRgn $drawable,@bounds,0,0;
  33.       my $dst = new PixelRgn $drawable,@bounds,1,1;
  34.  
  35.       $iter = Gimp->pixel_rgns_register ($src, $dst);
  36.  
  37.       do {
  38.          # get the pixels ($pixels will be modified in-place!)
  39.          $pixels = $src->data;
  40.  
  41.          # extract the rgb portion only
  42.          $rgb = $pixels->slice("0:2");
  43.  
  44.          # calculate difference to destination colour
  45.          $diff = 255 + minimum $rgb - pdl $colour;
  46.  
  47.          # adjust alpha part
  48.          my $alpha = $pixels->slice("(3)");
  49.          $alpha .= 255-$diff;
  50.  
  51.          # adjust the colour
  52.          my $a = ($diff/(255**2))->slice("*3") * pdl $colour;
  53.          $rgb .= 255-(255-$rgb) / (1-$a);
  54.  
  55.          # write the pixels into dst
  56.          $dst->data($pixels);
  57.  
  58.          Gimp->progress_update (($src->y-$bounds[1])/$bounds[2]);
  59.       } while (Gimp->pixel_rgns_process ($iter));
  60.    }
  61.    Gimp->progress_update (1);
  62.  
  63.    $drawable->merge_shadow (1);
  64.    $drawable->update ($drawable->bounds);
  65.  
  66.    ();        # wir haben kein neues Bild erzeugt
  67. };
  68.  
  69. exit main;
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.